package com.ujiuye.controller;

import com.ujiuye.service.IOrderListService;
import com.ujiuye.service.IOrderService;
import com.ujiuye.utils.*;
import com.ujiuye.vo.BusinessVo;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单-会员下单预约检查 前端控制器
 * </p>
 *
 * @author zhangwenping
 * @since 2021-07-05
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Reference
    private IOrderService orderService;
    @Reference
    private IOrderListService orderListService;

    @GetMapping
    @PreAuthorize("hasAnyAuthority('REPORT_VIEW')")
    public Result listSetmealCount(){
        try {
            List<Map<String, Object>> maps = orderService.listSetmealCount();
            return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,maps);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }

    @GetMapping("/business")
    public Result business(){
        try {
            BusinessVo businessVo = orderService.getBusinessVo();
            return new Result(true, MessageConstant.GET_BUSINESS_REPORT_SUCCESS,businessVo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }

    @PostMapping("/list")
    public PageResult orderList(@RequestBody QueryPageBean queryPageBean){
        try {
            return orderListService.listOrder(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @PostMapping("/listByWeek")
    public PageResult listByweek(@RequestBody QueryPageBean queryPageBean){
        try {
            return orderListService.listOrderByWeek(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @PostMapping("/listByMonth")
    public PageResult listByMonth(@RequestBody QueryPageBean queryPageBean){
        try {
            return orderListService.listOrderByMonth(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @PutMapping("/update/{orderId}")
    public Result update(@PathVariable("orderId") int orderId){
        boolean b = orderListService.updateOrderByOrderId(orderId);
        if (b){
            return new Result(true,"更新到诊状态成功");
        }else {
            return new Result(false,"更新到诊状态失败");
        }
    }

    @Value("${uploadPath}")
    private String uploadPath;

    @GetMapping("/businessExcel")
    public ResponseEntity<byte[]> businessExcel() throws IOException {
        //从数据库中查询数据
        BusinessVo businessVo = orderService.getBusinessVo();
        //读取表格
        FileInputStream inputStream = new FileInputStream(uploadPath+"business.xlsx");
        //创建workbook
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        //获取表
        XSSFSheet sheet = workbook.getSheetAt(0);
        //获取第三行
        XSSFRow row3 = sheet.getRow(2);
        row3.getCell(1).setCellValue(businessVo.getTodayNewMember());
        row3.getCell(3).setCellValue(businessVo.getTotalMember());
        //获取第四行
        XSSFRow row4 = sheet.getRow(3);
        row4.getCell(1).setCellValue(businessVo.getThisWeekNewMember());
        row4.getCell(3).setCellValue(businessVo.getThisMonthNewMember());
        //获取第六行
        XSSFRow row6 = sheet.getRow(5);
        row6.getCell(1).setCellValue(businessVo.getTodayOrderNumber());
        row6.getCell(3).setCellValue(businessVo.getTodayVisitsNumber());
        //获取第七行
        XSSFRow row7 = sheet.getRow(6);
        row7.getCell(1).setCellValue(businessVo.getThisWeekOrderNumber());
        row7.getCell(3).setCellValue(businessVo.getThisWeekVisitsNumber());
        //获取第八行
        XSSFRow row8 = sheet.getRow(7);
        row8.getCell(1).setCellValue(businessVo.getThisMonthOrderNumber());
        row8.getCell(3).setCellValue(businessVo.getThisMonthVisitsNumber());

        //热门套餐
        List<Map<String, Object>> hotSetmeal = businessVo.getHotSetmeal();
        for (int i=0;i<hotSetmeal.size();i++){
            Map<String, Object> map = hotSetmeal.get(i);
            XSSFRow row = sheet.getRow(i + 10);
            row.getCell(0).setCellValue((String)map.get("name"));
            row.getCell(2).setCellValue((Long)map.get("setmeal_count"));
            double proportion = ((BigDecimal) map.get("proportion")).doubleValue();
            row.getCell(3).setCellValue(proportion);
        }
        //把内存中的表格写到excel中
        FileOutputStream fileOutputStream = new FileOutputStream(uploadPath + "business.xlsx");
        workbook.write(fileOutputStream);
        //下载
        return MyFileUtils.download(new File(uploadPath+"business.xlsx"));
    }


}

